package com.ewhale.feitengguest.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.ewhale.feitengguest.R;
import com.ewhale.feitengguest.presenter.WebViewPresenter;
import com.ewhale.feitengguest.view.WebViewView;
import com.ewhale.feitengguest.widget.ProgressWebView;
import com.simga.library.activity.MBaseActivity;
import com.simga.library.utils.CheckUtil;
import com.simga.library.widget.BGButton;

import butterknife.BindView;


public class WebViewActivity extends MBaseActivity<WebViewPresenter> implements WebViewView {

    @BindView(R.id.web_view)
    ProgressWebView webView;

    private String titleStr = "", url = "";

    public static void open(Context context, String title, String url) {
        Intent intent = new Intent(context, WebViewActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("title", title);
        bundle.putString("url", url);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }


    @Override
    protected void onGetBundle(Bundle bundle) {
        if (bundle != null) {
            titleStr = bundle.getString("title");
            url = bundle.getString("url");
        }
    }

    @Override
    protected int getViewId() {
        return R.layout.activity_webview;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        if (titleStr.length() > 6) {
            setTitle(titleStr.substring(0, 6) + "...");
        } else {
            setTitle(titleStr);
        }
        webView.defaultSetting();
        //加载需要显示的网页
        if (CheckUtil.checkURL(url) || CheckUtil.checkURL(url)) {
            webView.loadUrl(url);
        } else {
            String content;
            if (!TextUtils.isEmpty(url) && !url.contains("html")) {
                String head = "<head>" +
                        "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\"> " + "<style>img{max-width: 100%; width:auto; height:auto;} body{word-break:break-all;}</style>" + "</head>";
                content = "<html>" + head + url + "</html>";
            } else {
                content = url;
            }
            webView.loadDataWithBaseURL(null, content, "text/html", "UTF-8", null);
        }
    }

    @Override
    protected void initListener() {
    }

    @Override
    protected boolean isUseBaseTitleBar() {
        return true;
    }

    @Override
    public void showProLoading() {
        showLoading();
    }

    @Override
    public void dimissProLoading() {
        dismissLoading();
    }

    @Override
    public void showErrorMessage(int postCode, String code, String message) {
        showErrorMsg(code, message);
    }
}
